<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 11-8-21
 * Time: 上午12:34
 */
 
class SVNClient{

    public function __construct($username = '',$password = ''){

        svn_auth_set_parameter ( SVN_AUTH_PARAM_DEFAULT_USERNAME ,  $username );
        svn_auth_set_parameter ( SVN_AUTH_PARAM_DEFAULT_PASSWORD ,  $password );
        svn_auth_set_parameter(PHP_SVN_AUTH_PARAM_IGNORE_SSL_VERIFY_ERRORS, true); // <--- Important for certificate issues!
        svn_auth_set_parameter(SVN_AUTH_PARAM_NON_INTERACTIVE,              true);
        svn_auth_set_parameter(SVN_AUTH_PARAM_NO_AUTH_CACHE,                true);
    }

    /**
     * 获取SVN上的某个文件保存到指定目录
     * @param $file svn文件
     * @param $to_dir 保存的目录
     * @param $version 要获取的版本号，默认取最新版本
     * @return bool
     */
    public function getFile($file,$to_dir,$version = SVN_REVISION_HEAD) {
        if($file && is_dir($to_dir)){
            $file_name = basename($file);
            if($file_name){
                $contents = svn_cat($file,$version);
                $contents = str_replace("\r\n","\n",$contents);
                $to_dir = rtrim($to_dir,DS);
                //return file_put_contents($to_dir . DS . $file_name,$contents);
                if (!$handle = fopen($to_dir . DS . $file_name, 'wb')) {
                     return false;
                }
                // 将$contents写入到我们打开的文件中。
                if (fwrite($handle, $contents) === FALSE) {
                    return false;
                }
                fclose($handle);
                return true;

            }
        }

        return false;
    }

    /**
     * 获取某个文件的某个版本前的行信息
     * @param $file
     * @param $version 默认为最新版本前的所有版本
     * @return array
     * Array (
     *  [0] => Array (
     *      [rev] => 2
     *      [line_no] => 1
     *      [line] => <?php
     *      [author] => fifsky
     *      [date] => 2011-08-20T15:41:53.875000Z
     *  )
     * [1] => Array (
     *      [rev] => 3
     *      [line_no] => 2
     *      [line] => //1
     *      [author] => fifsky
     *      [date] => 2011-08-20T15:52:45.640625Z
     *  )
     * )
     */
    public function getFileLineInfo($file,$version = SVN_REVISION_HEAD){

        return svn_blame($file,$version);
    }

    /**
     * 获取某个文件的信息
     * @param $file
     * @param $version 查看的版本，默认查看最新版本
     * @return array
     * Array (
     *  [svn.php] => Array (
     *      [created_rev] => 5
     *      [last_author] => fifsky
     *      [size] => 25
     *      [time] => Aug 21 00:59
     *      [time_t] => 1313859570
     *      [name] => svn.php
     *      [type] => file
     *  )
     * )
     */
    public function getFileInfo($file,$version = SVN_REVISION_HEAD){
        try{
            $ret = svn_ls($file,$version);
            return $ret;
        }catch(Exception $e){
            $error = $e->getMessage();
            $data = array(
                "statusCode"=>"610",
                "message"=>$error,
            );
            echo Response::JSON($data);
            exit;
        }
    }

    public function getFileAllInfo($file){
        return svn_ls($file);
    }

    /**
     * 获取某个文件的SVN日志
     * @param $file svn文件
     * @param $version 版本号，默认是最新版
     * @param int $limit 取该版本前的10个版本
     * @return array
     * Array (
     *  [0] => Array (
     *      [rev] => 3
     *      [author] => fifsky
     *      [msg] => 修改了2
     *      [date] => 2011-08-20T15:52:45.640625Z
     *      [paths] => Array (
     *          [0] => Array (
     *              [action] => M
     *              [path] => /svn.php
     *              )
     *          )
     *      )
     * )
     *       Actions
     * Letter 	Description
     *  M 	Item/props was modified
     *  A 	Item was added
     *  D 	Item was deleted
     *  R 	Item was replaced
     */
    public function getFileLog($file,$limit = 10,$version = SVN_REVISION_HEAD){
        return svn_log($file,$version,0,$limit);
    }

    /**
     * 比较某个文件的两个版本
     * @param $file svn文件
     * @param $version1 版本1
     * @param $version2 版本2，默认最新版
     * @return string
     * Index: svn.php ===================================================================
     * --- svn.php (revision 2)
     * +++ svn.php (revision 6)
     * @@ -1,2 +1,4 @@
     * <?php
     * -//1
     * \ No newline at end of file
     * +//2
     * +//3
     * +//4
     * \ No newline at end of file
     */
    public function getFileDiff($file,$version1,$version2 = SVN_REVISION_HEAD){

        list($diff, $errors) = svn_diff(
            $file, $version1,
            $file, $version2
        );
        if (!$diff) exit;
        $contents = '';
        while (!feof($diff)) {
            $contents .= fread($diff, 8192);
        }
        fclose($diff);
        fclose($errors);
        return $contents;
    }
}